﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2013/11/10 星期日 20:01:17
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sector7.Framework.Drivers.RF;
using Sector7.Medical.Biz;
using Sector7.Framework.Core;
using Sector7.Framework.Enums;
using Sector7.Framework.Utility;

namespace Sector7.Medical.Utility
{
    /// <summary>
    /// <!-- The description about this class. -->
    /// </summary>
    public class RfReaderUtil
    {
        #region Public functions
        public static string GetSerialNumber()
        {
            string serialNumber = string.Empty;

            IRfReaderBase reader = new RfReader_MW_EYE_U010();
            try
            {
                reader.Initialize(Menvironments.PORT, Menvironments.BAUD);
                reader.LoadPassword(RfReader_MW_EYE_U010.KEY_MODE_A0, Menvironments.SECTOR_NUMBER, string.Empty);
                reader.SearchCard(RfReader_MW_EYE_U010.SCAN_MODE_REPEAT);

                serialNumber = reader.GetSerialNumber();

                reader.ReleaseCard();
                reader.Beep(RfReader_MW_EYE_U010.BEEP_DURATION_SUCCESS);
            }
            catch (RfReaderException e)
            {
                reader.Beep(RfReader_MW_EYE_U010.BEEP_DURATION_FAILURE);

                Alert.ShowDialog(AlertType.Error, e.ErrorMessage);
            }
            finally
            {
                reader.Dispose();
            }

            return serialNumber;
        }

        /// <summary>
        /// 读取卡片数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static int ReadData(out string data)
        {
            int result = RfReader_ErrorCode.ERR_RF_SUCCESS;
            data = string.Empty;

            IRfReaderBase reader = new RfReader_MW_EYE_U010();
            try
            {
                reader.Initialize(Menvironments.PORT, Menvironments.BAUD);
                reader.LoadPassword(RfReader_MW_EYE_U010.KEY_MODE_A0, Menvironments.SECTOR_NUMBER, string.Empty);
                reader.SearchCard(RfReader_MW_EYE_U010.SCAN_MODE_REPEAT);

                byte[] dataBuffer = new byte[32];
                reader.ReadData(Menvironments.BLOCK_NUMBER, dataBuffer);
                data = StringUtil.BytesToString(dataBuffer);

                reader.ReleaseCard();
                reader.Beep(RfReader_MW_EYE_U010.BEEP_DURATION_SUCCESS);
            }
            catch (RfReaderException e)
            {
                reader.Beep(RfReader_MW_EYE_U010.BEEP_DURATION_FAILURE);
                result = e.ErrorCode;

                Alert.ShowDialog(AlertType.Error, e.ErrorMessage);
            }
            finally
            {
                reader.Dispose();
            }

            return result;
        }

        /// <summary>
        /// 写入卡片数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static int WriteData(string data)
        {
            int result = RfReader_ErrorCode.ERR_RF_SUCCESS;
            data = string.Empty;

            IRfReaderBase reader = new RfReader_MW_EYE_U010();
            try
            {
                reader.Initialize(Menvironments.PORT, Menvironments.BAUD);
                reader.LoadPassword(RfReader_MW_EYE_U010.KEY_MODE_A0, Menvironments.SECTOR_NUMBER, string.Empty);
                reader.SearchCard(RfReader_MW_EYE_U010.SCAN_MODE_REPEAT);

                byte[] dataBuffer = StringUtil.StringToBytes(data);
                reader.WriteData(Menvironments.BLOCK_NUMBER, dataBuffer);

                reader.ReleaseCard();
                reader.Beep(RfReader_MW_EYE_U010.BEEP_DURATION_SUCCESS);
            }
            catch (RfReaderException e)
            {
                reader.Beep(RfReader_MW_EYE_U010.BEEP_DURATION_FAILURE);
                result = e.ErrorCode;

                Alert.ShowDialog(AlertType.Error, e.ErrorMessage);
            }
            finally
            {
                reader.Dispose();
            }

            return result;
        }
        #endregion

        #region Private functions
        #endregion
    }
}
